﻿using System.Collections.Generic;
using System.Web.Mvc;
using CodeCamp.Models.Schedule;
using CodeCamp.Entities02;
using System.Linq;

namespace CodeCamp.Controllers
{
  public class ScheduleController : Controller
  {

    public ActionResult Agenda( string eventName )
    {
      int eventId = EventsHelper.GetEventId( eventName );
      var repository = new CodeCampDataRepository();
      var eventDisplayName = repository.GetEvent( eventId ).DisplayName;

      var model = new ScheduleAgendaViewModel { EventDisplayName = eventDisplayName };

      return View( model );
    }


    public ActionResult Index( string eventName )
    {
      int eventId = EventsHelper.GetEventId( eventName );
      var repository = new CodeCampDataRepository();
      var eventDisplayName = repository.GetEvent( eventId ).DisplayName;

      var timeslotList = new List<ScheduleIndexViewModel.Timeslot>();
      var timeslots = repository.GetTimeslots( eventId );
      foreach ( var timeslot in timeslots )
      {
        var sessionList = new List<ScheduleIndexViewModel.Timeslot.Session>();
        var scheduledSessions = timeslot.ScheduledSessions.OrderBy( ss => ss.Session.Title );
        foreach ( var scheduledSession in scheduledSessions )
        {
          var session = scheduledSession.Session;

          var presenters =
            (
              from sp in session.SessionPresenters
              orderby sp.Presenter.LastName
              select new ScheduleIndexViewModel.Timeslot.Session.Presenter
              {
                PressenterId = sp.PresenterId,
                Name = string.Format( "{0} {1}", sp.Presenter.FirstName, sp.Presenter.LastName )
              }
            ).ToList();

          sessionList.Add(
            new ScheduleIndexViewModel.Timeslot.Session
            {
              SessionId = session.Id,
              Title = SessionsController.FilterDevSatTag( session.Title ),
              Presenters = presenters,
              Building = scheduledSession.Room.Building.Name,
              Room = scheduledSession.Room.Name,
              RoomId = scheduledSession.RoomId
            } );
        }

        timeslotList.Add(
          new ScheduleIndexViewModel.Timeslot
          {
            TimeslotId = timeslot.Id,
            Name = timeslot.Name,
            Date = timeslot.Date,
            StartTime = DateTimeHelper.CombineDateAndTime( timeslot.Date, timeslot.StartTime ),
            EndTime = DateTimeHelper.CombineDateAndTime( timeslot.Date, timeslot.EndTime ),
            Sessions = sessionList
          } );
      }

      var model = 
        new ScheduleIndexViewModel
        { 
          EventDisplayName = eventDisplayName,
          Timeslots = timeslotList
        };

      return View( model );
    }

    public ActionResult Rooms( string eventName )
    {
      int eventId = EventsHelper.GetEventId( eventName );
      var repository = new CodeCampDataRepository();
      var eventDisplayName = repository.GetEvent( eventId ).DisplayName;

      var buildings =
        ( 
          from building in repository.GetBuildings( eventId )
          orderby building.Name
          select new ScheduleRoomsViewModel.Building
          {
            Name = building.Name,
            Rooms =
              (
                from room in building.Rooms
                orderby room.Name
                select new ScheduleRoomsViewModel.Building.Room
                {
                  RoomId = room.Id, 
                  Name = room.Name,

                  Sessions =
                  (
                    from scheduledSession in room.ScheduledSessions
                    orderby scheduledSession.Timeslot.StartTime
                    select new ScheduleRoomsViewModel.Building.Room.Session
                    {
                      SessionId = scheduledSession.SessionId,
                      Title = SessionsController.FilterDevSatTag( scheduledSession.Session.Title ),
                      StartTime = DateTimeHelper.CombineDateAndTime( scheduledSession.Timeslot.Date, scheduledSession.Timeslot.StartTime ),
                      EndTime = DateTimeHelper.CombineDateAndTime( scheduledSession.Timeslot.Date, scheduledSession.Timeslot.EndTime ),

                      Presenters =
                      (
                        from presenter in scheduledSession.Session.SessionPresenters
                        orderby presenter.Presenter.LastName
                        select new ScheduleRoomsViewModel.Building.Room.Session.Presenter
                        {
                          PresenterId = presenter.PresenterId,
                          Name = string.Format( "{0} {1}", presenter.Presenter.FirstName, presenter.Presenter.LastName )
                        }
                      ).ToList()
                    }
                  ).ToList()
                }
              ).ToList()
            } 
          ).ToList();
  
      var model = new ScheduleRoomsViewModel
      { 
        EventDisplayName = eventDisplayName,
        Buildings = buildings
      };

      return View( model );
    }

    public ActionResult Room( string eventName, int id )
    {
      int eventId = EventsHelper.GetEventId( eventName );
      var repository = new CodeCampDataRepository();
      var eventDisplayName = repository.GetEvent( eventId ).DisplayName;

      var room = repository.GetRoom( id );

      var sessions =
        (
          from scheduledSession in room.ScheduledSessions
          orderby scheduledSession.Timeslot.StartTime
          select new ScheduleRoomViewModel.Session
          {
            SessionId = scheduledSession.SessionId,
            Title = SessionsController.FilterDevSatTag( scheduledSession.Session.Title ),
            StartTime = DateTimeHelper.CombineDateAndTime( scheduledSession.Timeslot.Date, scheduledSession.Timeslot.StartTime ),
            EndTime = DateTimeHelper.CombineDateAndTime( scheduledSession.Timeslot.Date, scheduledSession.Timeslot.EndTime ),

            Presenters =
            (
              from presenter in scheduledSession.Session.SessionPresenters
              orderby presenter.Presenter.LastName
              select new ScheduleRoomViewModel.Session.Presenter
              {
                PresenterId = presenter.PresenterId,
                Name = string.Format( "{0} {1}", presenter.Presenter.FirstName, presenter.Presenter.LastName )
              }
            ).ToList()
          }
        ).ToList();

      var model = new ScheduleRoomViewModel
      {
        EventDisplayName = eventDisplayName,
        Building = room.Building.Name,
        Room = room.Name,
        Sessions = sessions
      };

      return View( model );
    }

    public ActionResult Hotels( string eventName )
    {
      int eventId = EventsHelper.GetEventId( eventName );
      var repository = new CodeCampDataRepository();
      var eventDisplayName = repository.GetEvent( eventId ).DisplayName;

      var model = new ScheduleHotelsViewModel { EventDisplayName = eventDisplayName };

      return View( model );
    }
  }
}
